#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <string.h> 
#include <stdio.h> 
#define MAXBUF 256 
//#define PORT 5000 
//#define GROUP "224.0.1.21" 

int main(void) {
	char tmp,GROUP[50]="224.0.1.21";
	int PORT=5000;
	printf("Set configuration now?(if you choose not, default configuration will be used.) y/n:");
	scanf("%c",&tmp);
	if(tmp=='y')
	{
		printf("Input group address:");
		scanf("%s",GROUP);
		printf("Input port number:");
		scanf("%d",&PORT);
	}
	
	int s; 
	struct sockaddr_in srv; 
	char buf[MAXBUF]; 
	bzero(&srv, sizeof(srv)); 
	srv.sin_family = AF_INET; 
	srv.sin_port = htons(PORT); 
	if (inet_aton(GROUP, &srv.sin_addr) < 0) { 
		perror("inet_aton"); 
		return 1; 
	} 
	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 
		perror("socket"); 
		return 1; 
	} 
	
	printf("Please input the message:");
	while (fgets(buf, MAXBUF, stdin)) { 
		if (sendto(s, buf, strlen(buf), 0,(struct sockaddr *)&srv, sizeof(srv)) < 0)  
			perror("recvfrom"); 
 		else 
//		printf("Send message %s to group: %s\n", buf,GROUP); 
		printf("Please input the message:");
	} 
} 
